<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/20
 * Time: 上午9:57
 */
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

//开启权限要在adminbasecontroller里面进行全线开启，如果字段名不匹配，就要在数据库改字段名或者到配置文件加别名
class GroupController extends AdminBaseController{
    public function index(){
        $manager = I('get.manager/s','');
        $page = I('get.page/d',1);
        $num = 5;
        $group = M('group');
        $sql = empty($manager) ? '' : 'title like "%'.$manager.'%"';
        $count = $group->where($sql)->count();
        $pagetotal = ceil($count/$num);
        $current = empty($page) ? 0 : (($page - 1) * $num);
        $list = $group->where($sql)->order('id desc')->limit($current,$num)->select();
        $this->assign('list',$list);
        $this->assign('manager',$manager);
        $this->assign('pagetotal',$pagetotal);
        $this->display();
    }

    public function rule(){
        if(!IS_POST){
            $id = I('get.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1,'msg' => '操作错误']);
            }
            $rules = M('Group')->field('rules')->where(['id' => $id])->find();
            $rulelist = M('Rule')->field('id,pid,title')->where('status = 1')->select();
            $data = make_tree($rulelist);
            unset($rulelist);
            $this->assign('id',$id);
            $this->assign('rules',explode(',',$rules['rules']));
            $this->assign('dataList',$data);
            $this->display();
        }else{
            $id = I('post.id/d',0);
            $rule_ids = I('post.rule_ids/a',[]);
            $isUp = false;
            if($id && !empty($rule_ids)){
                $isUp = M('group')->where(['id' => $id])->setField(['rules' => implode(',',$rule_ids)]);
            }
            if($isUp){
                $this->ajaxReturn(['status' => 0,'msg' => '更新成功']);
            }else{
                $this->ajaxReturn(['status' => 1,'msg' => '更新失败']);
            }
        }
    }

    public function edit(){
        if(IS_GET){
            $id = I('get.id/d',0);
            if(!$id){
                $this->ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }
            $groupinfo = M('group')->field(['id,title,status'])->where(['id' => $id])->find();
            $this->assign('groupinfo',$groupinfo);
            $this->display();
        }
    }

    public function changeDel(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
            }
            $del = M('group')->where(['id' => $id])->delete();
            if($del){
                $this->ajaxReturn(['ststus' => 0,'msg' => '删除成功']);
            }else{
                $this->ajaxReturn(['status' => 1,'msg' => '删除失败']);
            }
        }else{
            $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
        }
    }

    public function save(){
        if(IS_POST || IS_AJAX){
            $id = I('post.id/d',0);
            if(empty($id)){
                $title = I('post.title/s','');
                if(!$title){
                    $this->ajaxReturn(['status' => 1, 'msg' => '分组不能为空']);
                }
                $status = I('post.status/d');
                $group = M('group');
                $admindata = $group->where(['title' => $title])->find();
                if($admindata){
                    $this->ajaxReturn(['status' => 1,'msg' => '分组已存在']);
                }
                $data = [
                    'title' => $title,
                    'status' => $status
                ];
                $mid = $group->add($data);
            }else{
                $title = I('post.title/s','');
                if(!$title){
                    $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
                }
                $status = I('post.status/d');
                $mid = M('group')->where(['id'=>$id])->setField(['title' => $title,'status' => $status]);
            }
            if($mid === false){
                $this->ajaxReturn(['status' => 1, 'msg' => '失败']);
            }
            $this->ajaxReturn(['status' => 0, 'msg' => '成功']);
        }
    }

    public function changeMode(){
        if(IS_POST){
            $id = I('post.id/d','');
            if(empty($id)){
                $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
            }
            $query = M('group')->field('status')->where(['id' => $id])->find();
            if(empty($query['status'])){
                M('group')->where(['id' => $id])->setField('status','1');
                $this->ajaxReturn(['status' => 0,'msg' => '已开启','data' => 1]);
            }else{
                M('group')->where(['id' => $id])->setField('status','0');
                $this->ajaxReturn(['status' => 0,'msg' => '已关闭','data' => 0]);
            }
        }else{
            $this->ajaxReturn(['status' => 1,'msg' => '非法操作']);
        }
    }
}